home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / context.c < prev    next >
Text File  |  1993-12-06  |  4KB  |  153 lines

  1. /**
  2.  ** CONTEXT.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "clipping.h"
  27. #include "gmalloc.h"
  28.  
  29. #include <string.h>
  30.  
  31. GrContext *GrCreateContext(int w,int h,char far *memory,GrContext *where)
  32. {
  33.     long size   = GrContextSize(w,h);
  34.     long psize  = GrPlaneSize(w,h);
  35.     int  offset = GrLineOffset(w);
  36.     int  flags  = 0;
  37.  
  38.     if(size == 0L) return(NULL);
  39. #ifdef _MAXMEMPLANESIZE
  40.     if(psize > _MAXMEMPLANESIZE) return(NULL);
  41. #endif
  42.     if(where == NULL) {
  43.         where = _GrMalloc(sizeof(GrContext));
  44.         if(where == NULL) return(NULL);
  45.         flags |= GCM_MYCONTEXT;
  46.     }
  47.     memset(where,0,sizeof(GrContext));
  48.     if(memory == (char far *)NULL) {
  49.         memory = _GrFarMalloc(size);
  50.         if(memory == (char far *)NULL)
  51.         { if(flags) _GrFree(where); return(NULL); }
  52.         flags |= GCM_MYMEMORY;
  53.     }
  54.     where->gc_baseaddr    = memory;
  55.     where->gc_lineoffset  = offset;
  56.     where->gc_frameaddr   = BASE_ADDRESS(where);
  57.     where->gc_memflags    = flags;
  58.     where->gc_planeoffset = (long)((char huge *)where->gc_planeoffset + psize);
  59.     where->gc_xmax = where->gc_xcliphi = w - 1;
  60.     where->gc_ymax = where->gc_ycliphi = h - 1;
  61.     return(where);
  62. }
  63.  
  64. #undef  WHEN_OUTSIDE
  65. #define WHEN_OUTSIDE    return(NULL)
  66.  
  67. GrContext *GrCreateSubContext
  68.       (int x1,int y1,int x2,int y2,GrContext *parent,GrContext *where)
  69. {
  70.     int flags = 0;
  71.  
  72.     if(parent == NULL) parent = CURC;
  73.     if(parent->gc_root != NULL) {
  74.         x1 += parent->gc_xoffset;
  75.         y1 += parent->gc_yoffset;
  76.         x2 += parent->gc_xoffset;
  77.         y2 += parent->gc_yoffset;
  78.         parent = parent->gc_root;
  79.     }
  80.     CLIPBOXTOCONTEXT(parent,x1,y1,x2,y2);
  81.     if(where == NULL) {
  82.         where = _GrMalloc(sizeof(GrContext));
  83.         if(where == NULL) return(NULL);
  84.         flags |= GCM_MYCONTEXT;
  85.     }
  86.     *where = *parent;
  87.     where->gc_frameaddr = PIX_ADDR(parent,x1,y1);
  88.     where->gc_memflags  = flags;
  89.     where->gc_xoffset   = x1;
  90.     where->gc_yoffset   = y1;
  91.     where->gc_xcliphi   = where->gc_xmax = x2 - x1;
  92.     where->gc_ycliphi   = where->gc_ymax = y2 - y1;
  93.     where->gc_xcliplo   = 0;
  94.     where->gc_ycliplo   = 0;
  95.     where->gc_root        = parent;
  96.     return(where);
  97. }
  98.  
  99. #undef  WHEN_OUTSIDE
  100. #define WHEN_OUTSIDE    return
  101.  
  102. void GrResizeSubContext(GrContext *context,int x1,int y1,int x2,int y2)
  103. {
  104.     GrContext *parent;
  105.  
  106.     if((parent = context->gc_root) == NULL) return;
  107.     x1 += context->gc_xoffset;
  108.     y1 += context->gc_yoffset;
  109.     x2 += context->gc_xoffset;
  110.     y2 += context->gc_yoffset;
  111.     CLIPBOXTOCONTEXT(parent,x1,y1,x2,y2);
  112.     context->gc_frameaddr = PIX_ADDR(parent,x1,y1);
  113.     context->gc_xoffset   = x1;
  114.     context->gc_yoffset   = y1;
  115.     context->gc_xcliphi   = context->gc_xmax = x2 - x1;
  116.     context->gc_ycliphi   = context->gc_ymax = y2 - y1;
  117.     context->gc_xcliplo   = 0;
  118.     context->gc_ycliplo   = 0;
  119. }
  120.  
  121. void GrDestroyContext(GrContext *cxt)
  122. {
  123.     if((cxt != NULL) && (cxt != CURC) && (cxt != SCREEN)) {
  124.         if(cxt->gc_memflags & GCM_MYMEMORY)  _GrFarFree(cxt->gc_baseaddr);
  125.         if(cxt->gc_memflags & GCM_MYCONTEXT) _GrFree(cxt);
  126.     }
  127. }
  128.  
  129. void GrSetContext(GrContext *context)
  130. {
  131.     if(context == NULL) context = SCREEN;
  132.     _GrContext = *context;
  133.     _GrMouseCheck =
  134.         (_GrMouseDrawn && (CURC->gc_baseaddr == SCREEN->gc_baseaddr)) ?
  135.         TRUE :
  136.         FALSE;
  137. }
  138.  
  139. GrContext *GrSaveContext(GrContext *where)
  140. {
  141.     int flags = 0;
  142.  
  143.     if(where == NULL) {
  144.         where = _GrMalloc(sizeof(GrContext));
  145.         if(where == NULL) return(NULL);
  146.         flags |= GCM_MYCONTEXT;
  147.     }
  148.     *where = _GrContext;
  149.     where->gc_memflags = flags;
  150.     return(where);
  151. }
  152.  
  153.